package com.hemerasolutions.v2trainer.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.hemerasolutions.v2trainer.component.interfaces.list.StepListLevelsInterface;
import com.hemerasolutions.v2trainer.component.interfaces.search.TrainingSearchCompInterface;
import com.hemerasolutions.v2trainer.controller.interfaces.TrainingBeanInterface;
import com.hemerasolutions.v2trainer.delegate.interfaces.TrainingSearchDelegateInterface;
import com.hemerasolutions.v2trainer.model.step.Step;
import com.hemerasolutions.v2trainer.model.training.Training;
import com.hemerasolutions.v2trainer.model.training.TrainingStep;
import com.hemerasolutions.v2trainer.service.ExceptionService;
import com.hemerasolutions.v2trainer.service.interfaces.TrainingServiceInterface;

@Controller
@ManagedBean(name="trainingBean")
@Scope("view")
public class TrainingBean extends BaseBean implements TrainingBeanInterface,TrainingSearchDelegateInterface  {

	private static final long serialVersionUID = 1L;
	
	private List<Training> trainings;
	
	private Training selTraining;
	
	private Training training;

	@Autowired
	private TrainingServiceInterface trainingService;

	@Autowired
	private StepListLevelsInterface levelsHandler;
	
	@Autowired
	private TrainingSearchCompInterface trainingSearchComp;
	
	public TrainingBean() {
		super();
		
	}

	@PostConstruct
	public void init(){
		this.training = null;
		this.trainingSearchComp.setTrainingSearchDelegate(this);
		this.trainingSearchComp.refresh();
		this.listAll();
	}
	
	
	private void listAll() {
	   	Map<String,Object> fields = new HashMap<String, Object>();
    	
    		fields.put("librarys",this.getActiveUserAllowedLibrarys());
    		fields.put("trainingSystem",false);
    		
		    this.trainings = this.trainingService.searchTrainings(fields);		
		
	}

	public void clear(){
		this.training = null;
	}

	
	public void toViewTraining(){
		for(TrainingStep ts :this.training.getTrainingSteps()){
			this.collapseAll(ts.getRootStep().getSteps());
		}
		this.levelsHandler.setStepLevels(this.training.getTrainingSteps());
		this.levelsHandler.refresh();		

	}
	
	private void collapseAll(List<Step> steps) {
		for(Step step: steps){
			if(step.getType() > Step.REPETITION_STEP){
				this.collapseAll(step.getSteps());
			}else{
				step.setMark(true);
			}
		}
	}

	public void createTraining(){
		try {
			this.training.setCreatedAt(new Date());
			this.training.setChangedBy(this.getActiveUser());
			this.training.setLastChange(new Date());
			this.training = trainingService.createTraining(training);
			this.showInfoMessage(	this.localizedText("CreatedWithSuccess"),
					this.localizedText("TheTraining") + " " +
					this.training.getName() + "\n , " + 
					this.localizedText("wasCreatedWithSuccess"));			
			this.listAll();

		} catch (ExceptionService e) {
			this.showErrorMessage(this.localizedText("CreateError"), e.getLocalizedMessage());
			e.printStackTrace();
		}
	}
	
	
	public void saveTraining(){
		try {
			trainingService.saveTraining(training);
			this.training.setChangedBy(this.getActiveUser());
			this.training.setLastChange(new Date());
			this.showInfoMessage(	this.localizedText("SavedWithSuccess"),
					this.localizedText("TheTraining") + " " +
					this.training.getName() + "\n , " +
					this.localizedText("wasSavedWithSuccess"));			
			this.listAll();
		} catch (ExceptionService e) {
			this.showErrorMessage(this.localizedText("SaveError"), e.getLocalizedMessage());
		}
	}
	
	public void cancelChange(){
		if(this.training.getId() != 0){
			this.training = trainingService.readTraining(training);
		}else{
			this.training = new Training();
		}
	}	
	
	
	public void deleteTraining(){
		
		try{
			trainingService.deleteTraining(training);
			this.showInfoMessage(	this.localizedText("Deleted"),
									this.localizedText("TrainingDeletedWithSuccess"));
			this.trainings.remove(this.training);
			this.listAll();
		}catch(ExceptionService e){
			this.showErrorMessage(this.localizedText("DeleteError"),
					e.getLocalizedMessage());
		}
		
	}
	
	
	public void toAddTraining(){
		this.training  = new Training();
		this.training.setTrainingSystem(false);
		this.training.prepareLevels();

		this.levelsHandler.setStepLevels(this.training.getTrainingSteps());
		this.levelsHandler.refresh();		
	
	}

	public List<Training> getTrainings() {
		return trainings;
	}

	public void setTrainings(List<Training> trainings) {
		this.trainings = trainings;
	}

	public Training getSelTraining() {
		return selTraining;
	}

	public void setSelTraining(Training selTraining) {
		this.selTraining = selTraining;
	}

	public Training getTraining() {
		return training;
	}

	public void setTraining(Training training) {
		this.training = training;
	}

	public StepListLevelsInterface getLevelsHandler() {
		return levelsHandler;
	}

	public void setLevelsHandler(StepListLevelsInterface levelsHandler) {
		this.levelsHandler = levelsHandler;
	}

	
	public TrainingSearchCompInterface getTrainingSearchComp() {
		return trainingSearchComp;
	}

	public void setTrainingSearchComp(TrainingSearchCompInterface trainingSearchComp) {
		this.trainingSearchComp = trainingSearchComp;
	}

	@Override
	public boolean onTrainingAdd(Training training) {
		return false;
	}

	@Override
	public void onTrainingImportSteps(List<Step> steps) {
	}

	@Override
	public void onSearchComplete(List<Training> trainings) {
		this.trainings = new ArrayList<Training>();
		this.trainings.addAll(trainings);
	}

	
}
